<?php
class ArticulosController extends AppController {

	var $name = 'Articulos';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";
		if($this->JQGridPaginate->isGrid){
			$fields = array('Articulo.id','Articulo.codigo','Articulo.descripcion', 'Articulo.presentacion',
							'Articulo.unidadxcaja','Articulo.peso',
							'Articulo.costo_soles','Articulo.costo_dolares','Articulo.costo_base_soles','Articulo.costo_base_dolares',
							'Articulo.costo_margen','Articulo.costo_descuento',
							'"Unidad"."descripcion" AS "Articulo__unidad"',
							'"Familia"."descripcion" AS "Articulo__familia"',
							'"Articulo"."created"::date AS "Articulo__created"',
							'"Estado"."descripcion" AS "Articulo__estado"');
			
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			$this->autoRender = false;
			echo $this->JQGridPaginate->paginate("Articulo");
		}elseif ($this->RequestHandler->isPost()){			
			$this->autoRender = false;
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
			    $this->data['Articulo']['organizacion_id'] = 1;
				$this->Articulo->create();				 
			}elseif ($oper == "edit"){
				$this->data['Articulo']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Articulo']['id'] = $this->params['form']['id'];
				$this->data['Articulo']['estado_id'] = 2;			
			}
			
			@$this->Articulo->save($this->data);
			echo json_encode($this->Articulo->visualizarError($this->Articulo->validationErrors));
			$this->autoRender = false;
		}		
	}	
	
	
	function getArticulosLista(){
		Configure::write('debug',1);
		$articulosLista = $this->Articulo->getArticuloListaJson();
		echo $articulosLista;
		$this->autoRender = false;
	}
	
	function getArticulosListaJson(){
		$articulosLista = $this->Articulo->getArticuloListaJson($this->params['url']['term']);
		echo $articulosLista;
		$this->autoRender = false;
	}
}